﻿using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{

    public class UIPop : BaseUI
    {
        [SerializeField]
        private UIPopText mNode;
        private Queue<PopText> mQueue = new Queue<PopText>();
        private FloatTimeCounter mTimeCounter;

        private Canvas layerControl;
        private bool isShowLoading = false;

        public float popInterval = 0.4f;
        public float buttleInterval = 0.4f;

        private static Stack<UIPopText> mNodeList = new Stack<UIPopText>();

        public override bool OnInit(object arg)
        {
            mTimeCounter = new FloatTimeCounter();
            return true;
        }

        protected  void OnEnable()
        {
            EventMa.Register<UIPopTextArgs>(UIPopTextArgs.EventId, Show);
            //GEM.instance.AddEvent<bool>(GameEventEnum.Show_Loading, OnLoadingShow);
        }

        protected  void OnDisable()
        {
            EventMa.Unregister<UIPopTextArgs>(UIPopTextArgs.EventId, Show);
            //GEM.instance.RemoveEvent<bool>(GameEventEnum.Show_Loading, OnLoadingShow);
        }

        void OnLoadingShow(bool state)
        {
            isShowLoading = state;
        }

        public void Show(UIPopTextArgs args)
        {
            mQueue.Enqueue(new PopText()
            {
                IsBullet = args.isBulletPop,
                text = args.text
            });

            // ShowNode();
            if (layerControl == null)
            {
                layerControl = GetComponent<Canvas>();
            }
            layerControl.sortingOrder = 8000;
        }

        protected void Update()
        {
            if (mQueue.Count > 0 && !isShowLoading)
            {
                ShowNode();
            }
        }

        private void ShowNode()
        {
            if (mTimeCounter == null)
            {
                return;
            }

            if (mTimeCounter.GetPassTime() > 0.4f)
            {
                PopText pop = mQueue.Dequeue();
                UIPopText node = GetNode();
                if (node != null)
                {
                    node.gameObject.SetActive(true);
                    node.SetInfo(pop);
                }

                mTimeCounter.ResetCounter();
            }


        }

        private UIPopText GetNode()
        {
            if (mNodeList.Count > 0)
            {
                UIPopText node = mNodeList.Pop();
                if (node != null)
                {
                    node.transform.SetAsLastSibling();
                }

                return node;
            }
            else
            {
                UIPopText node = Instantiate(mNode) as UIPopText;
                if (node != null)
                {
                    if (!node.gameObject.activeInHierarchy)
                    {
                        node.gameObject.SetActive(true);
                    }

                    node.transform.SetParent(transform);
                    node.transform.localScale = Vector3.one;
                }

                return node;
            }
        }

        public static void ReuseNode(UIPopText node)
        {
            node.gameObject.SetActive(false);
            mNodeList.Push(node);
        }

    }


    public class PopText
    {
        public bool IsBullet = false;
        public string text;
    }
}
